package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.MemKey;
import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.conn.MemcacheConn;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.entity.UsersHXEntity;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class Users extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1572326059846802291L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		System.out.println(postData.toString());
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "pwd":
			String old_password = data.getString("old_password");
			String repassword = data.getString("repassword");
			String password = data.getString("password");
			int userId = data.getInt("user_id");
			if (password.length() < 6) {
				e = "密码太弱";
			} else if (!password.equals(repassword)) {
				e = "密码不一致";
			} else {
				UsersEntity u = UsersService.getInstance().findUserById(userId);
				if (u == null) {
					e = "用户不存在";
				} else {
					if (!u.getPassword().equals(old_password)) {
						e = "原密码错误";
					} else {
						u.setPassword(password);
						if (UsersService.getInstance().updateUsers(u)) {
							r = 1;
						} else {
							e = "系统错误";
						}
					}
				}
			}
			break;
		case "register":
			String phone = data.getString("phone");
			String code = data.getString("code");
			int is_doc = 0;
			if (phone != null) {
				String mem_code = MemcacheConn.getInstance().get(MemKey.Regist_CODE_ + phone);
				if (code != null && code.equals(mem_code)) {
					UsersEntity u = new UsersEntity();
					u.setLoginname("unset");
					u.setPassword("unset");
					u.setPhone(phone);
					u.setRegister_time(new Timestamp(System.currentTimeMillis()));
					u.setStatus(1);
					u.setIs_doc(is_doc);
					boolean existPhone = UsersService.getInstance().existPhone(phone);
					if (existPhone) {
						e = "该手机号已经存在";
					} else {
						int newId = UsersService.getInstance().registerUser(u);
						if (newId > -1) {
							r = 1;
							JSONObject d0 = new JSONObject();
							d0.put("id", newId);
							json.put("d", d0);
						} else {
							e = "系统错误";
						}
					}

				} else {
					e = "验证码错误";
				}
			} else {
				e = "手机号为null";
			}
			break;
		case "login_by_phone":
			phone = data.getString("phone");
			code = data.getString("code");
			String login_code = MemcacheConn.getInstance().get(MemKey.Login_CODE_ + phone);
			if (code != null && code.equals(login_code)) {
				UsersEntity users = UsersService.getInstance().findUserByPhone(phone);
				if (users != null) {
					UsersHXEntity hx = UsersService.getInstance().findHXUserByUserId(users.getId());
					JSONObject d = new JSONObject();
					d.put("id", users.getId());
					d.put("loginname", users.getLoginname());
					d.put("is_doc", users.getIs_doc());
					d.put("register_time", StrUtil.getYYYYMMDD(users.getRegister_time().getTime()));
					d.put("last_login_time", StrUtil.getYYYYMMDD(users.getLast_login_time().getTime()));
					d.put("hx_username", hx.getUsername());
					d.put("hx_password", hx.getPassword());
					d.put("head_url", SystemConfig.SERVER_DOMAIN + users.getHead_url());
					r = 1;
					json.put("d", d);
				} else {
					e = "该用户不存在";
				}
			} else {
				e = "验证码错误";
			}
			break;
		case "import":
			phone = data.getString("phone");
			UsersEntity u = new UsersEntity();
			u.setLoginname("unset");
			u.setPassword("unset");
			u.setPhone(phone);
			u.setRegister_time(new Timestamp(System.currentTimeMillis()));
			u.setStatus(1);
			u.setIs_doc(0);
			boolean existPhone = UsersService.getInstance().existPhone(phone);
			if (existPhone) {
				e = "该手机号已经存在";
			} else {
				int newId = UsersService.getInstance().registerUser(u);
				if (newId > -1) {
					r = 1;
					JSONObject d0 = new JSONObject();
					d0.put("id", newId);
					json.put("d", d0);
				} else {
					e = "系统错误";
				}
			}
			break;
		case "login_by_account":
			System.out.println("111111");
			String loginname = data.getString("loginname");
			password = data.getString("password");
			if (loginname == null || "".equals(loginname) || "unset".equals(loginname)) {
				e = "用户名无效";
				break;
			}
			if (password == null || "".equals(password) || "unset".equals(password)) {
				e = "密码错误";
				break;
			}
			UsersEntity users = UsersService.getInstance().findByLoginname(loginname, password);
			System.out.println("222222");
			if (users != null) {
				UsersHXEntity hx = UsersService.getInstance().findHXUserByUserId(users.getId());
				JSONObject d = new JSONObject();
				d.put("id", users.getId());
				d.put("loginname", users.getLoginname());
				d.put("is_doc", users.getIs_doc());
				d.put("register_time", StrUtil.getYYYYMMDD(users.getRegister_time().getTime()));
				d.put("last_login_time", StrUtil.getYYYYMMDD(users.getLast_login_time().getTime()));
				d.put("hx_username", hx.getUsername());
				d.put("hx_password", hx.getPassword());
				d.put("head_url", SystemConfig.SERVER_DOMAIN + users.getHead_url());
				r = 1;
				json.put("d", d);
			} else {
				e = "该用户不存在";
			}
			System.out.println("333333");
			break;
		case "set_account":
			loginname = data.getString("loginname");
			password = data.getString("password");
			int id = data.getInt("id");
			if (loginname == null || "".equals(loginname) || "unset".equals(loginname)) {
				e = "用户名无效";
				break;
			}
			if (password == null || "".equals(password) || "unset".equals(password)) {
				e = "密码错误";
				break;
			}

			if (UsersService.getInstance().findByLoginname(loginname) != null) {
				e = "登陆用户名被占用";
				break;
			} else {
				users = UsersService.getInstance().findUserById(id);
				users.setLoginname(loginname);
				users.setPassword(password);

				if (UsersService.getInstance().updateUsers(users)) {
					r = 1;
				} else {
					e = "系统错误";
				}
			}
			break;
		case "change_phone":
			id = data.getInt("id");
			phone = data.getString("phone");
			code = data.getString("code");
			String change_code = MemcacheConn.getInstance().get(MemKey.Change_CODE_ + phone);
			if (code != null && code.equals(change_code)) {
				if (UsersService.getInstance().findUserByPhone(phone) == null) {
					users = UsersService.getInstance().findUserById(id);
					users.setPhone(phone);
					boolean b = UsersService.getInstance().updateUsers(users);
					if (b) {
						r = 1;
					} else {
						e = "服务器错误";
					}
				} else {
					e = "该手机号已经占用";
				}
			} else {
				e = "验证码错误";
			}
			break;
		case "set_head_icon":
			String head_url = data.getString("head_url");
			int user_id = data.getInt("user_id");
			users = UsersService.getInstance().findUserById(user_id);
			users.setHead_url(head_url);
			if (UsersService.getInstance().updateUsers(users)) {
				r = 1;
			} else {
				e = "服务器错误";
			}

			break;
		}
		json.put("r", r);
		json.put("e", e);

		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
